var util = require('../utils/util');
var daoUtil = require('../utils/daoUtil');
var scmodel = require('../class/stockChannel');

module.exports = {
    initChannel:function(params){
        var channel = {};
        channel.createUid = params.createUid||"";
        channel.stockId = params.stockId||"";
        channel.stockGroupId = params.stockGroupId;
        channel.channelType = params.channelType;
        // channel.cost = params.cost;
        // channel.positionNum = params.positionNum;
        channel.enable = 1;
        return channel;
    },
    create: function (params, cb) {
        var validate = daoUtil.validate(['stockId', 'createUid','channelType'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            var schannel = new scmodel();
            schannel.init(params);
            models.stock_channel.create(schannel.getParams()).exec(function (err, data) {
                if (err || !data) {
                    console.log(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    update: function (params, cb) {
        var validate = daoUtil.validate(['rowId', 'cost'|'positionNum'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            var schannel = new scmodel();
            schannel.init(params);
            models.stock_channel.update({rowId: params.rowId}, {cost:params.cost,positionNum:params.positionNum}).exec(function (err, data) {
                if (err || !data) {
                    console.log(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    getListByChannelType:function (params,cb) {
        var validate = daoUtil.validate(['channelType'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        params.enable = 1;
        daoUtil.proxy(function(models){
            models.stock_channel.find(params,{sort:'createdAt desc'}).populate('createUid').populate('stockId').populate('industry').populate('stockEstimates').exec(function (err, data) {
                if (err || !data) {
                    console.log(err);
                    cb(util.getSysError());
                }else{

                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    get:function (params,cb) {
        var validate = daoUtil.validate(['rowId'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            models.stock_channel.findOne({rowId:params.rowId}).populate('createUid').populate('stockId').populate('industry').exec(function (err, data) {
                if (err || !data) {
                    console.log(err);
                    cb(util.getSysError());
                }else{

                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    //按实盘分组id查询股票信息
    getByGroupId:function (params,cb) {
        var validate = daoUtil.validate(['stockGroupId'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            models.stock_channel.find({'stockGroupId':params.stockGroupId,'enable':1,'positionNum':{'!':0}}).populate('stockGroupId').populate('stockId').exec(function (err, data) {
                if (err || !data) {
                    console.log(err);
                    cb(util.getSysError());
                }else{

                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    //按实盘分组id和股票ID查询股票信息
    getByStockIdAndGroupId:function (params,cb) {
        var validate = daoUtil.validate(['stockGroupId','stockId','channelType'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            models.stock_channel.findOne({'stockGroupId':params.stockGroupId,'stockId':params.stockId,'enable':1,channelType:params.channelType}).populate('stockGroupId').populate('stockId').exec(function (err, data) {
                if (err) {
                    console.log(err);
                    cb(util.getSysError());
                }else{

                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    findAllStock:function(params,cb){
        var sql = "select (select stockCode from tb_dic_stock where a.stockId = rowId) stockCode from tb_stock_channel a where a.enable = 1 group by a.stockId ";
        daoUtil.proxy(function(models) {
            models.stock_channel.query(sql, function (err, data) {
                if (err || !data) {
                    console.error(err);
                    cb(util.getSysError());
                } else {
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    // list: function (params, cb) {
    //     var query = {};
    //     if (params.createUid)
    //         query.createUid = params.createUid;
    //     if(params.fileType)
    //         query.fileType = params.fileType;
    //     if(params.channelId && params.channelId.length >0)
    //         query.channelId = params.channelId;
    //     daoUtil.proxy(function(models){
    //         models.stock_channel.find({where:query}).populate('createUid').populate('stockId').exec(function (err, data) {
    //             if (err || !data) {
    //                 console.log(err);
    //                 cb(util.getSysError());
    //             }else{
    //                 cb(null, util.getSuccessRes(data));
    //             }
    //         });
    //     },cb);
    // },
    deleteByIds:function (rowIds, cb) {
        daoUtil.proxy(function(models){
            models.stock_channel.destroy({rowId:rowIds}).exec(function (err, result) {
                if (err) {
                    console.log(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(result));
                }
            });
        },cb);
    }
};